package com.md.youyouhui.ui.login;

import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.md.youyouhui.R;
import com.md.youyouhui.databinding.FragmentLoginBinding;
import com.md.youyouhui.net.Constants;
import com.md.youyouhui.net.response.LoginResponse;
import com.md.youyouhui.ui.BaseFragment;
import com.md.youyouhui.ui.MainActivity;
import com.md.youyouhui.utils.AppManager;
import com.md.youyouhui.utils.SharedPreferencesUtils;
import com.md.youyouhui.utils.ToastUtils;
import com.md.youyouhui.view.MainViewModel;

/**
 * Created by AdministratorZJ
 * on 2019/4/3.
 */
public class LoginFragment extends BaseFragment {
    private FragmentLoginBinding loginBinding;
    private MainViewModel viewModel;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        loginBinding = DataBindingUtil.inflate(inflater, R.layout.fragment_login, container, false);
        viewModel = new MainViewModel();
        initData1();
        return loginBinding.getRoot();
    }

    private void initData1() {
        loginBinding.loginBtn.setOnClickListener(v -> {
            loginNext();
        });
        loginBinding.loginMsg.setOnClickListener(v -> ((LoginActivity) mActivity).msgLogin());
        loginBinding.forget.setOnClickListener(v -> startActivity(new Intent(mActivity, ForgetOneActivity.class)));
    }

    private void loginNext() {
        String phone = loginBinding.loginPhone.getText().toString();
        if (TextUtils.isEmpty(phone)) {
            Toast.makeText(mActivity, "请输入手机号", Toast.LENGTH_SHORT).show();
            return;
        }
        String pass = loginBinding.loginPass.getText().toString();
        if (TextUtils.isEmpty(pass)) {
            Toast.makeText(mActivity, "请输入密码", Toast.LENGTH_SHORT).show();
            return;
        }
        //类型:1.验证码登录2.密码登录
        viewModel.login(2, phone, "", pass, "android").observe(this, loginResponse -> {
            assert loginResponse != null;
            switch (loginResponse.status) {
                case LOADING:
                    loginBinding.progress.setVisibility(View.VISIBLE);
                    break;
                case SUCCESS:
                    loginBinding.progress.setVisibility(View.GONE);
                    LoginResponse data = loginResponse.data;
                    assert data != null;
                    SharedPreferencesUtils.saveString(mActivity, Constants.USER_TOKEN, data.token);
                    SharedPreferencesUtils.saveString(mActivity, Constants.INVITED_CODE, data.invitedCode);
                    //((LoginActivity) mActivity).requestLogin();
                    AppManager.getAppManager().finishAllActivity();
                    startActivity(new Intent(mActivity, MainActivity.class));
                    if (null == getActivity()) {
                        return;
                    }
                    getActivity().finish();
                    break;
                case ERROR:
                    loginBinding.progress.setVisibility(View.GONE);
                    ToastUtils.errMake(mActivity, loginResponse.errorCode);
                    break;
                default:
                    loginBinding.progress.setVisibility(View.GONE);
                    break;
            }
        });
    }

    public void forgetPass() {
        startActivity(new Intent(mActivity, ForgetOneActivity.class));
    }
}
